﻿using IM.Easy.Infrastructure.Query;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Extensions
{
    public static class SqlSugarQueryExtensions
    {
        /// <summary>
        /// 读取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">查询表单式</param>
        /// <param name="parm">分页参数</param>
        /// <returns></returns>
        public static PageResponse<T> ToPageListAsync<T>(this ISugarQueryable<T> source, QueryBase parm) where T : class, new()
        {
            var page = new PageResponse<T>();
            var total = 0;
            page.PageSize = parm.PageSize;
            page.PageIndex = parm.PageIndex;
            if (!string.IsNullOrEmpty(parm.Sort))
            {
                source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc", StringComparison.OrdinalIgnoreCase) ? OrderByType.Desc : OrderByType.Asc);
            }
            page.Result = source
                .ToPageList(parm.PageIndex, parm.PageSize, ref total);
            page.TotalNum = total;
            return page;
        }

        public static PageResponse<T> ToPageList<T>(this ISugarQueryable<T> source, QueryBase parm) where T : class, new()
        {
            var page = new PageResponse<T>();
            var total = 0;
            page.PageSize = parm.PageSize;
            page.PageIndex = parm.PageIndex;
            if (!string.IsNullOrEmpty(parm.Sort))
            {
                source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc", StringComparison.OrdinalIgnoreCase) ? OrderByType.Desc : OrderByType.Asc);
            }
            page.Result = source
                .ToPageList(parm.PageIndex, parm.PageSize, ref total);
            page.TotalNum = total;
            return page;
        }

        /// <summary>
        /// 转指定实体类Dto
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        public static PageResponse<T2> ToPageListAsync<T1, T2>(this ISugarQueryable<T1> source, QueryBase parm) where T1 : class, new() where T2 : class, new()
        {
            var page = new PageResponse<T2>();
            var total = 0;
            page.PageSize = parm.PageSize;
            page.PageIndex = parm.PageIndex;
            if (!string.IsNullOrEmpty(parm.Sort))
            {
                source.OrderByPropertyName(parm.Sort, parm.SortType.ToLower().Contains("desc", StringComparison.OrdinalIgnoreCase) ? OrderByType.Desc : OrderByType.Asc);
            }
            var result = source
                .ToPageList(parm.PageIndex, parm.PageSize, ref total);
            page.TotalNum = total;
            page.Result = result.Adapt<List<T2>>();
            return page;
        }

        /// <summary>
        /// 转指定实体类Dto
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        public static PageResponse<T2> ToPageList<T1, T2>(this ISugarQueryable<T1> source, QueryBase parm) where T1 : class, new() where T2 : class, new()
        {
            var page = new PageResponse<T2>();
            var total = 0;
            page.PageSize = parm.PageSize;
            page.PageIndex = parm.PageIndex;
            if (!string.IsNullOrEmpty(parm.Sort))
            {
                source.OrderByPropertyName(parm.Sort, parm.SortType.Contains("desc", StringComparison.OrdinalIgnoreCase) ? OrderByType.Desc : OrderByType.Asc);
            }
            var result = source
                .ToPageList(parm.PageIndex, parm.PageSize, ref total);
            page.TotalNum = total;
            page.Result = result.Adapt<List<T2>>();
            return page;
        }

        /// <summary>
        /// 转指定实体类Dto
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        public static List<T2> ToList<T1, T2>(this ISugarQueryable<T1> source)
        {
            var result = source.ToList();
            var list = result.Adapt<List<T2>>();
            return list;
        }

    }
}